package gui;

import javax.swing.*;

import multiformat.BinaryBase;
import multiformat.DecimalBase;
import multiformat.FixedPointFormat;
import multiformat.FloatingPointFormat;
import multiformat.FormatException;
import multiformat.HexBase;
import multiformat.NumberBaseException;
import multiformat.OctalBase;

import java.awt.*;
import java.awt.event.*;

public class CalculatorController extends JPanel implements ActionListener{
	CalculatorModel model;	
	
	private JRadioButton decimal = new JRadioButton();	
	private JRadioButton binary = new JRadioButton();
	private JRadioButton octal = new JRadioButton();
	private JRadioButton hexa = new JRadioButton();
	private JRadioButton fixed = new JRadioButton();
	private JRadioButton floating = new JRadioButton();	
	
	private JTextField input = new JTextField(15);
	private JTextArea output = new JTextArea(5, 15);	
	
	private JButton add = new JButton(" + ");
	private JButton sub = new JButton(" - ");
	private JButton mul = new JButton(" x ");
	private JButton dev = new JButton(" / ");
	
	public CalculatorController(CalculatorModel c){
		
		model = c;
		
		setLayout(new BorderLayout());	
		
		Container selections = new Container();
		selections.setLayout(new GridLayout(6, 1));	
		
		//Add labels to radio buttons
		decimal.setLabel("Dec");
		binary.setLabel("Bin");
		octal.setLabel("Oct");
		hexa.setLabel("Hex");
		fixed.setLabel("Fixed");
		floating.setLabel("Float");			
		
		selections.add(decimal);
		selections.add(binary);
		selections.add(octal);
		selections.add(hexa);
		selections.add(fixed);
		selections.add(floating);
		
		//Add actionlisteners
		decimal.addActionListener(this);
		binary.addActionListener(this);
		octal.addActionListener(this);
		hexa.addActionListener(this);		
		fixed.addActionListener(this);
		floating.addActionListener(this);
		
		//Create a new group for the radio buttons of the bases
		ButtonGroup bases = new ButtonGroup();
		bases.add(decimal);
		bases.add(binary);
		bases.add(octal);
		bases.add(hexa);
		decimal.setSelected(true);
		
		//Create new group for the radio buttons of the formats
		ButtonGroup formats = new ButtonGroup();
		formats.add(fixed);
		formats.add(floating);
		fixed.setSelected(true);
		
		this.add(selections, BorderLayout.WEST);
		
		Container inputOutput = new Container();
		inputOutput.setLayout(new GridLayout(2,1));
		inputOutput.add(input);
		input.addActionListener(this);
		input.requestFocus();
		
		input.setPreferredSize(new Dimension(20,20));
		inputOutput.add(output);
		output.setEditable(false);	
		output.setAutoscrolls(true);
		
		this.add(inputOutput, BorderLayout.CENTER);		
		
		Container buttons = new Container();
		buttons.setLayout(new GridLayout(4, 1));
		
		buttons.add(add);
		add.addActionListener(this);		
		buttons.add(sub);
		sub.addActionListener(this);		
		buttons.add(mul);
		mul.addActionListener(this);
		buttons.add(dev);
		dev.addActionListener(this);
		
		this.add(buttons, BorderLayout.EAST);
	}
	
	public void actionPerformed(ActionEvent e){		
		if(e.getSource() == input){
			try {
				model.addOpperand(input.getText());
				String inputString = input.getText();
				output.insert(inputString + "\n", 0);			
				input.setText("");
			} catch (NumberBaseException e1) {				
				output.insert("De invoer komt niet overeen met het talstelsel\n", 0);
			} catch (FormatException e1) {				
				output.insert("De invoer komt niet overeen met de format\n",0);
			}			
			
		}
		else if(e.getSource() != input){
			
			String firstOp = model.firstOperand();
			String secondOp = model.secondOperand();
			String operation = "";
			String option = ""; 
			
			if(e.getSource() == add){
				operation = "operator";
				option = "+";				
				model.add();				
			}
			if(e.getSource() == sub){
				operation = "operator";
				option = "-";
				model.substract();				
			}
			if(e.getSource() == mul){
				operation = "operator";
				option = "x";				
				model.multiply();				
			}
			if(e.getSource() == dev){
				operation = "operator";
				option = "/";
				model.divide();				
			}
			if(e.getSource() == decimal){
				operation = "base";
				option = "decimaal";
				model.setBase(new DecimalBase());				
			}
			if(e.getSource() == binary){
				operation = "base";
				option = "binair";
				model.setBase(new BinaryBase());				
			}
			if(e.getSource() == octal){
				operation = "base";
				option = "octaal";
				model.setBase(new OctalBase());				
			}
			if(e.getSource() == hexa){
				operation = "base";
				option = "hexadecimaal";
				model.setBase(new HexBase());				
			}
			if(e.getSource() == fixed){
				operation = "format";
				option = "fixed";
				model.setFormat(new FixedPointFormat());				
			}
			if(e.getSource() == floating){
				operation = "format";
				option = "floating";
				model.setFormat(new FloatingPointFormat());				
			}			
			
			model.setOutputString(operation, option, firstOp, secondOp);	
			output.insert(model.getOutputString(), 0);
		}
		
		
				
	}
}